/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.wiki.domain;

import javax.persistence.*;
import java.util.*;

import foo.bar.wiki.services.ServicesUtil;

/**
 * Represents a page.
 *
 * @author tmjee
 * @version $Date$ $Id$
 */
@Entity
@Table(name="TBL_PAGE")
@EntityListeners({EntityAuditListener.class})
@NamedQueries({
    @NamedQuery(
        name="Page_findPagesBySpaceId",
        query="SELECT page FROM Page AS page WHERE page.space.id = :spaceId"),
    @NamedQuery(
        name="Page_countPagesBySpaceId",
        query="SELECT count(page) FROM Page AS page WHERE page.space.id = :spaceId"),
    @NamedQuery(
        name="Page_findRecentlyUpdatedPages",
        query="SELECT page FROM Page AS page ORDER BY page.modificationDate DESC, page.creationDate DESC"),
    @NamedQuery(
        name="Page_countRecentlyUpdatedPages",
        query="SELECT count(page) FROM Page AS page"),
    @NamedQuery(
        name="Page_findPageById",
        query="SELECT page FROM Page AS page WHERE page.id = :pageId"),
    @NamedQuery(
        name="Page_findPageByName",
        query="SELECT page FROM Page AS page WHERE page.name = :pageName"),
    @NamedQuery(
        name="Page_findPageBySpaceIdAndPageName",
        query="SELECT page FROM Page AS page WHERE page.name = :pageName AND page.space.id = :spaceId "),
    @NamedQuery(
        name="Page_findLatestPagesBySpaceId",
        query="SELECT page FROM Page AS page WHERE page.space.id = :spaceId ORDER BY page.modificationDate DESC, page.creationDate DESC"
    )
})
public class Page implements Identifiable, CreationInfoPersistable, ModificationInfoPersistable, PathAware {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;

    @Basic
    @Column(name="NAME")
    private String name;

    @Basic
    @Column(name="DESCRIPTION")
    private String description;

    @Basic
    @Column(name="CONTENT")
    private String content;

    @ManyToOne(targetEntity = Space.class)
    @JoinColumn(name = "SPACE_ID", referencedColumnName = "ID")
    private Space space;

    @OneToMany(targetEntity = PageProperty.class, mappedBy = "page", cascade = {CascadeType.ALL})
    private List<PageProperty> properties = new ArrayList<PageProperty>();

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="MODIFIER_ID", referencedColumnName = "ID")
    private User modifier;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="CREATOR_ID", referencedColumnName = "ID")
    private User creator;

    @Basic
    @Column(name="MODIFICATION_DATE")
    private Date modificationDate;

    @Basic
    @Column(name="CREATION_DATE")
    private Date creationDate;

    @Basic
    @Column(name="PAGE_LOCATION")
    private String pageLocation = "/page/page.jsp";

    @OneToMany(targetEntity = Resource.class, mappedBy = "page", cascade = {CascadeType.ALL})
    private List<Resource> resources = new ArrayList<Resource>();

    @OneToMany(targetEntity = Comment.class, cascade = {CascadeType.ALL}, mappedBy = "page")
    private List<Comment> comments = new ArrayList<Comment>();


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<PageProperty> getProperties() {
        if (properties == null) {
            setProperties(new ArrayList<PageProperty>());
        }
        return properties;
    }

    public void setProperties(List<PageProperty> properties) {
        this.properties = properties;
    }

    public void setModifier(User modifier) {
        this.modifier = modifier;
    }

    public User getModifier() {
        return modifier;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public User getCreator() {
        return creator;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public String getPageLocation() {
        return pageLocation;
    }

    public void setPageLocation(String pageLocation) {
        this.pageLocation = pageLocation;
    }

    public List<Resource> getResources() {
        if (resources == null) {
            setResources(new ArrayList<Resource>());
        }
        return resources;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }


    public Space getSpace() {
        return space;
    }

    public void setSpace(Space space) {
        this.space = space;
    }


    public List<Comment> getComments() {
        if (comments == null) {
            setComments(new ArrayList<Comment>());
        }
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }





    public long getTotalComments() {
        return comments.size();
    }




    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Page page = (Page) o;

        if (name != null ? !name.equals(page.name) : page.name != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (properties != null ? properties.hashCode() : 0);
        result = 31 * result + (modificationDate != null ? modificationDate.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result + (pageLocation != null ? pageLocation.hashCode() : 0);
        result = 31 * result + (resources != null ? resources.hashCode() : 0);
        return result;
    }

    public String getFullPath() {
        String path = "/" + space.getName();
        path = path + "/" + name;
        return path;
    }
}


